package hyy_2022;

import commons.TreeNode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * 中序遍历
 * 今天牙不痛了,但是脸依旧肿,很满足了
 */
public class _0717_94_InorderTraversal {


    /**
     * 递归,中序遍历
     * @param root
     * @param res
     */
    public void inorder(TreeNode root, List<Integer> res) {
        if(root == null) {
            return;
        }

        inorder(root.left,res);
        res.add(root.val);
        inorder(root.right,res);

    }


    /**
     * 迭代法
     * @param root
     * @return List<Integer>
     */
    public List<Integer> inOrder(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> stack = new LinkedList<>();
        while(root != null || !stack.isEmpty()) {
            while(root != null) {
                stack.push(root);
                root = root.left;
            }

            root = stack.pop();
            res.add(root.val);
            root = root.right;
        }
        return res;
    }
}
